//
//  RTJWithdrawalController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/13.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJWithdrawalController.h"
#import "RTJForgetPassWordController.h"
#import "RTJWithdrawalResultController.h"
#import "RTJWithdrawSubsidiaryController.h"
#import "RTJChangeBankView.h"

@interface RTJWithdrawalController ()
@property (weak, nonatomic) IBOutlet UIView *backview;
@property (weak, nonatomic) IBOutlet UITextField *moenyText;
@property (weak, nonatomic) IBOutlet UILabel *shengyuLab;
@property (weak, nonatomic) IBOutlet UILabel *numLab;
@property (nonatomic,assign) NSInteger minMoney;
@property (weak, nonatomic) IBOutlet UILabel *bankName;
@property (nonatomic,strong)NSDictionary *data;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;
@property (nonatomic,strong) NSString *bankIdStr;
@end

@implementation RTJWithdrawalController

-(void) viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
     self.viewTop.constant = screenH == 812 ? 88 : 64;
    self.title = @"提现";
    
    UIButton *clear=[UIButton buttonWithType:UIButtonTypeSystem];
    clear.size=CGSizeMake(60, 30);
    [clear setTitle:@"提现记录" forState:UIControlStateNormal];
    [clear setTitleColor:[UIColor colorForHex:@"#666666"] forState:UIControlStateNormal];
    clear.titleLabel.font=[UIFont systemFontOfSize:14];
    [clear addTarget:self action:@selector(tixian) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:clear];
    
    
    _backview.layer.cornerRadius = 5;
    _backview.layer.shadowColor = [UIColor colorForHex:@"#dbc9cc"].CGColor;
    _backview.layer.shadowOffset = CGSizeMake(-1, 1);
    _backview.layer.shadowOpacity = 0.5;
    _backview.layer.shadowRadius = 5;
    
    [self userbanklist];
}

-(void)tixian{
    
    RTJWithdrawSubsidiaryController *vc = [[RTJWithdrawSubsidiaryController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

-(void)userbanklist{
    
    [SVProgressHUD showWithStatus:nil];
    [self.view showEmptyViewWithStr:@"" image:nil callback:nil];

    [RTJNetWork asyncRequestWithURL:@"user/cash" params:nil method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];

        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
            
                self.data = data[@"data"];
                self.bankName.text = [NSString stringWithFormat:@"%@（尾号 %@）",self.data[@"bank_list"][0][@"name"],self.data[@"bank_list"][0][@"bankcard_format"]];
                self.shengyuLab.text = [NSString stringWithFormat:@"账户可用金额：%@ 元",[RTJUserTool getTheUserModel].money_format];
                self.numLab.text = [NSString stringWithFormat:@"%@次",self.data[@"free_cash_num"]];
                self.minMoney = [self.data[@"min_cash_money"] integerValue];
                self.moenyText.placeholder = [NSString stringWithFormat:@"%ld元起提现",(long)self.minMoney];
                self.bankIdStr = [NSString stringWithFormat:@"%@",self.data[@"bank_list"][0][@"id"]];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"获取银行卡信息失败:%@",data[@"msg"]]];
                [self.view showEmptyViewWithStr:[NSString stringWithFormat:@"获取银行卡信息失败:%@",data[@"msg"]] image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self userbanklist];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self userbanklist];
            }];
        }
        
    }];
    
}

- (IBAction)tixian:(UIButton *)sender {
    
    NSInteger money = [_moenyText.text integerValue];
    
    if (money<self.minMoney) {
        
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%ld元起提现",(long)self.minMoney]];
        return;
        
    }
    
    SpecialAlertView *special = [[SpecialAlertView alloc]initWithLevelType:YES];
    
    [special withSureClick:^(NSInteger tags,NSString *password,UIView *alertView) {
        
        [UIView animateWithDuration:0.3 animations:^{
            [alertView removeFromSuperview];
        }];
        
        if (tags == 110) {
            
//            [UIView animateWithDuration:0.3 animations:^{
//                [alertView removeFromSuperview];
//            }];
            
            RTJForgetPassWordController *vc = [[RTJForgetPassWordController alloc]init];
            vc.isPayPassWord = YES;
            vc.title = @"找回支付密码";
            [self.navigationController pushViewController:vc animated:YES];
            
        }else{
            
            if (password.length  == 0) {
                
                [SVProgressHUD showErrorWithStatus:@"请输入交易密码"];
                return ;
            }
            //验证支付密码是否正确
            [RTJNetWork asyncRequestWithURL:@"user/check_pay_password" params:@{@"pay_password":password} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
                
                if (success) {
                    
                    if ([data[@"status"]integerValue]==1) {
                        
//                        [UIView animateWithDuration:0.3 animations:^{
//                            [alertView removeFromSuperview];
//                        }];
                        
                        //开始进行提现
                        [self withdrawal:password];
                        
                    }else{
                        
                        
                        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"密码错误:%@",data[@"msg"]]];
                    }
                    
                }else{
                    
                    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"密码错误:%@",errorMsg]];
                    
                }
                
                
                
            }];
            
        }
        
    }];
    
    
    
}

-(void)withdrawal:(NSString *)psd{//
    
    
    NSDictionary *dic = @{@"money":_moenyText.text,
                          @"paypassword":psd,
                          @"user_bank_id":_bankIdStr
                          };
    
    [RTJNetWork asyncRequestWithURL:@"user/carry" params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        if (success) {
            
            if ([data[@"status"]integerValue]==1) {
                
                [SVProgressHUD showSuccessWithStatus:@"提交成功"];
                
                RTJWithdrawalResultController *vc = [[RTJWithdrawalResultController alloc]init];
                vc.money = self.moenyText.text;
                [self.navigationController pushViewController:vc animated:YES];
                
            }else{
                
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"提交失败:%@",data[@"msg"]]];
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"提交失败:%@",errorMsg]];
            
        }
        
    }];
    
}

- (IBAction)changeBankCard:(id)sender {
    
    RTJChangeBankView *bankView = [[RTJChangeBankView alloc]initWithFrame:self.view.bounds];
    bankView.data =  self.data[@"bank_list"];
    bankView.isTiXian = YES;
    [bankView setBankClick:^(NSDictionary * dic) {
        
        self.bankName.text = [NSString stringWithFormat:@"%@（尾号 %@）",dic[@"name"],dic[@"bankcard_format"]];
        self.bankIdStr = [NSString stringWithFormat:@"%@",dic[@"id"]];
        
    }];
    bankView.VC = self;
    [bankView viewShow];
}

@end
